package com.hspedu.hspliving.commodity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hspedu.common.utils.PageUtils;
import com.hspedu.common.utils.Query;
import com.hspedu.hspliving.commodity.dao.AttrgroupDao;
import com.hspedu.hspliving.commodity.entity.AttrEntity;
import com.hspedu.hspliving.commodity.entity.AttrgroupEntity;
import com.hspedu.hspliving.commodity.service.AttrService;
import com.hspedu.hspliving.commodity.service.AttrgroupService;
import com.hspedu.hspliving.commodity.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("attrgroupService")
public class AttrgroupServiceImpl extends ServiceImpl<AttrgroupDao, AttrgroupEntity> implements AttrgroupService {
    @Autowired
    private AttrgroupDao attrgroupDao;
    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrgroupEntity> page = this.page(
                new Query<AttrgroupEntity>().getPage(params),
                new QueryWrapper<AttrgroupEntity>()
        );
        Integer count = attrgroupDao.selectCount(new LambdaQueryWrapper<>());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setTotalCount(count);
        return pageUtils;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        //检索参数
        String key = (String) params.get("key");
        QueryWrapper<AttrgroupEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(key)) {
            //精确查id或name模糊查
            //queryWrapper.eq("id", key).or().like("name", key);
            //(id = ? OR name LIKE ? AND category_id = ?)
            // 改成 ((id = ? OR name LIKE ?) AND (category_id = ?))
            queryWrapper.and((obj) -> {
                obj.eq("id", key).or().like("name", key);
            });
        }
        if (categoryId != 0) {
            queryWrapper.eq("category_id",categoryId);
        }
        IPage<AttrgroupEntity> page = this.page(
                new Query<AttrgroupEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    /**
     * getAttrGroupWithAttrsByCatelogId
     根据分类得到该分类下的所有属性分组和分组下的基本属性
     * 1、查出当前分类下的所有属性分组，
     * 2、查出每个属性分组的所有属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1、查询分组信息
        List<AttrgroupEntity> attrGroupEntities = this.list(new
                QueryWrapper<AttrgroupEntity>().eq("category_id", catelogId));
        //2、查询所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            //拷贝数据
            BeanUtils.copyProperties(group,attrsVo);
            //查询属性组id 对应基本属性list
            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

}
